class BroadcastsController < ApplicationController
  include ApplicationHelper
  
  before_filter :login_required, :except => [:show, :index, :search_tagged]
                :body_id
  
  def body_id
    @body_id = :program
  end

  # GET /broadcasts/new
  def new
    @broadcast = Broadcast.new
  	@languages = Broadcast::LANGUAGES
  	@genres = Broadcast::GENRES
  	@user_audio_assets = current_user.find_audio_assets
  	
  	@program = Program.find params[:program_id]
  end
  
  # POST /broadcasts
  # POST /broadcasts.:format
  def create
    @broadcast = Broadcast.new(params[:broadcast])
    
  	respond_to do |format|
      if !associate_audio_assets
        flash[:error] = "You have to choose, at least, one Audio Asset"
        format.html { redirect_to :action => 'new', :program_id => params[:broadcast][:program_id] }
  	  elsif @broadcast.save
  	    flash[:notice] = "Success on creating the new broadcast"
    		format.html { redirect_to :action => 'show', :id => @broadcast }
    		format.xml { head :ok }
  	  else
        flash[:error] = "Sorry, the broadcast can't be created. Try again"
    		format.html { redirect_to :action => 'new', :program_id => params[:broadcast][:program_id] }
    		format.xml { render :xml => @broadcast.errors.to_xml }
        end
  	end
  end
  
  # GET /broadcasts/:id
  # GET /broadcasts/:id.:format
  def show   
  	respond_to do |format|
      begin
        @broadcast = Broadcast.find params[:id]
    	  format.html { @tags = @broadcast.tag_cloud }
    	  format.xml { render :xml => @broadcast.to_xml( :root => 'broadcast', :include => :audio_assets ) }
    	rescue
        flash[:error] = "Sorry, the Broadcast doesn't exist"
        format.html { redirect_back_or_default(home_path) }
        format.xml { head :ok }
      end
  	end
  end
  
  # GET /broadcasts
  # GET /broadcasts.:format
  # GET /programs/:program_id/broadcasts
  # GET /programs/:program_id/broadcasts.:format
  def index
    @broadcasts = if params[:program_id]
      @program = Program.find params[:program_id]
      @program.broadcasts
    else
      Broadcast.find :all, :order => :name
    end

  	respond_to do |format|
  	  format.html
  	  format.xml { render :xml => @broadcasts.to_xml( :root => 'broadcasts' ) }
  	end
  end
  
  def tagging
    @broadcast = Broadcast.find params[:tagging][:resource_id]
    if @broadcast
      @broadcast.add_tags(params[:tagging][:tags])
      @broadcast.save
      
      @tags = @broadcast.tag_cloud
    else
      @tags = []
    end
    
    render :partial => 'shared/tag_cloud', :locals => { :tags => @tags, :controller => 'broadcasts' }
  end
  
  def search_tagged
    @broadcasts = Broadcast.find_by_tags params[:tag]
    @all_tags = Broadcast.tag_cloud
    @tag = params[:tag]
  end
  
  private
  def associate_audio_assets
    at_least_one_asset = false
    
    params[:assets].each do |asset, value|
  	  if value != ''
  	    audio_asset = AudioAsset.find value
  	    if audio_asset
          @broadcast.add_asset audio_asset 
          at_least_one_asset = true
        end
  	  end
  	end
    
    at_least_one_asset
  end
end
